<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$uniacid=$_SESSION['uniacid'];
$mid=$_SESSION['uid'];
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['basic']=$basic;
$keywords=$this->plugin_setting('search');
if($_RQ['op']=='main'){
	//exi('gg','error');
	$actid=$_RQ['id'];
	$help_mid=$_RQ['help_mid'];
	$act=pdo_get('jy_yousetli_act',array('uniacid'=>$uniacid,'id'=>$_RQ['id']));
	//从玩家表查询玩家数据
	$player=pdo_get('jy_yousetli_player',array('uniacid'=>$uniacid,'mid'=>$mid,'actid'=>$_RQ['id']));
	$member=pdo_get('core_members',array('uniacid'=>$uniacid,'id'=>$mid));
	$money_start=$act['money_start'];
	//获取token
	$token = $_SESSION['token'];
	$return['act']=$act;
	$return['player']=$player;
	$return['member']=$member;
	$return['params']=array();
	set_html('title','优惠接力活动');
	$return['sharedata']=array(
		'title'=>$basic['share_title'],
		'desc'=>$basic['share_desc'],
		'link'=>custom_url('jy_yousetli/index/'),
		'imgUrl'=>ATTACHMENT_URL.$basic['share_image'],
	);
	$helps_sum = $helps_sum?$helps_sum:0;
	if($info['discount_type']==1){
		$youhuizhi = $helps_sum.'元';
	}else{
		$youhuizhi = (10 - $helps_sum)<$info['discount_min']?$info['discount_min']:(10 - $helps_sum);
		$youhuizhi = $youhuizhi.'折';
	}
	$return['helps_sum']=$helps_sum;
	$return['youhuizhi']=$youhuizhi;
	include $this->template('game');
	//exi($return);
}
//游戏结束结算
if($_RQ['op']=='count'){
	$id=$_RQ['id'];
	$score=$_RQ['score'];
	$help_mid=$_RQ['help_mid'];
//	file_put_contents(ROOT_D.'/csss.txt','id='.$id.'$score='.$score.'$help_mid='.$help_mid);
	if($help_mid>0){
		//检查自己有无帮助他的记录
		$help=pdo_get('jy_yousetli_help',array('uniacid'=>$uniacid,'mid'=>$mid,'actid'=>$id,'help_mid'=>$help_mid));
		$wo=pdo_get('core_members',array('uniacid'=>$uniacid,'id'=>$mid));
		if(isset($help['id'])){
			if(!empty($help['xiangqing'])){
				$xiangqing=$help['xiangqing'].';'.$score;
			}else{
				$xiangqing=$score;
			}
			pdo_update('jy_yousetli_help',array('money +='=>$score,'xiangqing'=>$xiangqing,'play_times +='=>1),array('id'=>$help['id']));
		}else{
			$ins=array(
				'uniacid'=>$uniacid,
				'mid'=>$mid,
				'actid'=>$_RQ['id'],
				'money'=>$score,
				'play_times'=>1,
				'xiangqing'=>$score,
				'help_mid'=>$help_mid,
				'avatar'=>$wo['avatar'],
				'nickname'=>$wo['nickname'],
				'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_yousetli_help',$ins);
		}
	}else{
		$player=pdo_get('jy_yousetli_player',array('actid'=>$id,'mid'=>$mid));
		if(!empty($player['xiangqing'])){
			$xiangqing=$player['xiangqing'].';'.$score;
		}else{
			$xiangqing=$score;
		}
		pdo_update('jy_yousetli_player',array('money +'=>$score,'xiangqing'=>$xiangqing,'play_times +='=>1),array('actid'=>$id,'mid'=>$mid,'uniacid'=>$uniacid));
		//把自己的游戏数据也存入帮助表，并将状态修改为1，标识为自己的游戏数据
		$help=pdo_get('jy_yousetli_help',array('uniacid'=>$uniacid,'mid'=>$mid,'actid'=>$id,'help_mid'=>$mid));
		$wo=pdo_get('core_members',array('uniacid'=>$uniacid,'id'=>$mid));
		if(isset($help['id'])){
			if(!empty($help['xiangqing'])){
				$xiangqing=$help['xiangqing'].';'.$score;
			}else{
				$xiangqing=$score;
			}
			pdo_update('jy_yousetli_help',array('money'=>$help['money']+$score,'xiangqing'=>$xiangqing,'play_times +='=>1),array('id'=>$help['id']));
		}else{
			$ins2=array(
				'uniacid'=>$uniacid,
				'mid'=>$mid,
				'actid'=>$_RQ['id'],
				'money'=>$score,
				'play_times'=>1,
				'xiangqing'=>$score,
				'help_mid'=>$_SESSION['uid'],
				'avatar'=>$wo['avatar'],
				'nickname'=>$wo['nickname'],
				'status'=>'1',
				'createtime'=>TIMESTAMP
			
			);
			pdo_insert('jy_yousetli_help',$ins2);
		}
	}
	exi($return);
}
// 检查用户可游戏次数
if($_RQ['op']=='check'){
	$id=$_RQ['id'];
	$help_mid=$_RQ['help_mid'];
	//如果是帮助别人的
	if($help_mid){
		$help=pdo_get('jy_yousetli_help',array('uniacid'=>$uniacid,'mid'=>$mid,'actid'=>$_RQ['id'],'help_mid'=>$help_mid));
		$act=pdo_get('jy_yousetli_act',array('uniacid'=>$uniacid,'id'=>$_RQ['id']));
		$cishu=$act['friends_count']-$help['play_times'];
	//	file_put_contents(ROOT_D.'/csss.txt','help_mid='.$help_mid.';次数='.$cishu);
	}else{
		$player=pdo_get('jy_yousetli_player',array('actid'=>$id,'mid'=>$mid,'uniacid'=>$uniacid));
		//判断玩家的剩余游戏次数
		$cishu=$act['my_count']-$player['play_times']+$player['share_times'];
	}
	if($cishu<= 0){
		$return['error']=1;	
		$return['url']=custom_url('jy_yousetli/detail//id='.$_RQ['id'].'&help_mid='.$help_mid);	
		
	}
	exi($return);
}